import { reactive, ref } from 'vue'
import { defineStore } from 'pinia'
import { useFriendStore, useUserStore } from '@/stores'
// import { addNowChatService } from '@/api/chat.js'
export const useChatStore = defineStore('chat', () => {
    const verifyMsg = ref([])
    const setVerifyMsg = (data) => {
        if (data) {
            verifyMsg.value = data;
        }

    }

    const friendStore = useFriendStore();
    const userStore = useUserStore();
    const setNewFriend = (id) => {
            nowChatFriends.value.forEach(item => {
                if (item.id + '' === id + '') {
                    item.msg.push({
                        from: userStore.user.id,
                        text: '我们已经成为好友啦~快来聊天吧',
                        time: new Date().toLocaleString()
                    })
                    item.time = new Date().toLocaleString(),
                        item.overMsg = '我们已经成为好友啦~快来聊天吧'
                }
            })


        }
        //对话列表
    const nowChatFriends = ref([]);
    const setNowChatFriends = (data) => {
            let list = [];
            if (data) {
                data.forEach(friendBo => {
                    let nowChatEntity = {
                        id: friendBo.id,
                        img: friendBo.img,
                        name: friendBo.name,
                        time: '',
                        overMsg: '',
                        signature: friendBo.signature,
                        unread: 0,

                        msg: []
                    }
                    list.push(nowChatEntity);
                    // .push(nowChatEntity)
                });
                nowChatFriends.value = list;

            }
        }
        // const promise = (id) => {
        //     addNowChatService(id);
        // }

    const setNowChatFriensById = (id) => {

        let flag = true;
        nowChatFriends.value.forEach(nowChatFriend => {
            console.log('setNowChatFriensById', id)
            console.log('setNowChatFriensById', nowChatFriend.id)
            if (nowChatFriend.id + '' === id + '') {
                //对话列表已经存在用户
                console.log('已存在这个用户')
                flag = false;
            }
        })
        if (flag) {
            friendStore.friends.forEach(friend => {
                if (friend.id + '' === id + '') {
                    let nowChatEntity = {
                            id: friend.id,
                            img: friend.img,
                            name: friend.name,
                            time: '',
                            overMsg: '',
                            signature: friend.signature,
                            unread: 0,
                            msg: []
                        }
                        // promise(id)
                    nowChatFriends.value.unshift(nowChatEntity);
                }
            })


        }


    }
    const removeNowChatFriendById = (id) => {
            let removeEntity = nowChatFriends.value.findIndex(item => {
                if (item.id + '' === id + '') {
                    return true;
                }
            });
            nowChatFriends.value.splice(removeEntity, 1);
            chating.value.id = '';
            chating.value.img = '';
            chating.value.name = '';
            chating.value.msg = '';
        }
        //对话列表时间以及最后一句话
    const setNowChatTime = (id, time, msg) => {
            if (id && time && msg) {
                nowChatFriends.value.forEach(nowChat => {
                    if (nowChat.id === id) {
                        nowChat.time = time;
                        nowChat.overMsg = msg;
                    }
                })
            }

        }
        //聊天记录
    const setMsg = (data) => {
            let id = data.from;


        }
        //当前选中对话
    const chating = ref({
        id: '',
        name: '',
        img: '',
        msg: []
    })
    const setChating = (id) => {
            nowChatFriends.value.forEach(item => {
                if (item.id + '' === id + '') {
                    chating.value.id = item.id;
                    chating.value.name = item.name;
                    chating.value.img = item.img;
                    //直接赋值还不行，会把引用地址直接给
                    chating.value.msg = [];
                    item.msg.forEach(item => {
                        chating.value.msg.push(item)
                    })
                }
            })
        }
        //清除未读
    const removeunRead = (id) => {
        nowChatFriends.value.forEach(item => {
            if (item.id + '' === id + '') {
                item.unread = 0;
            }
        })
    }

    //离线消息
    const setOffLineMsg = (data) => {
            if (!data) {
                return;
            }
            data.forEach(item => {
                setNowChatFriensById(item.id);
                nowChatFriends.value.forEach(nowFriend => {
                    if (item.id + '' === nowFriend.id + '') {
                        nowFriend.overMsg = item.offlineRespList[item.offlineRespList.length - 1].text;
                        nowFriend.time = item.offlineRespList[item.offlineRespList.length - 1].time;
                        nowFriend.msg.push(item.offlineRespList)
                        if (chating.value.id + '' !== nowFriend.id + '') {
                            nowFriend.unread = item.offlineRespList.length;
                        } else {
                            chating.value.msg.push(item.offlineRespList);
                        }

                    }
                })

            })
        }
        //发送消息
    const setSendMsg = (fromId, msg) => {
            let sendMsgEntity = {
                from: fromId,
                text: msg,
                time: new Date().toLocaleString()
            }
            chating.value.msg.push(sendMsgEntity)
            nowChatFriends.value.forEach(item => {
                if (item.id + '' === chating.value.id + '') {
                    item.msg.push(sendMsgEntity);
                    item.overMsg = msg;
                    item.time = new Date().toLocaleString();
                }
            })


        }
        //接收消息
    const receiveMsg = (data) => {

            if (data.from + '' === chating.value.id + '') {
                chating.value.msg.push(data);
                nowChatFriends.value.forEach(item => {
                    if (item.id + '' === data.from + '') {
                        item.msg.push(data);
                        item.overMsg = data.text;
                        item.time = new Date().toLocaleString();
                    }
                })
            } else {
                setNowChatFriensById(data.from);
                nowChatFriends.value.forEach(item => {
                    if (item.id + '' === data.from + '') {
                        item.msg.push(data);
                        item.overMsg = data.text;
                        item.unread += 1;
                        item.time = new Date().toLocaleString();
                    }
                })
            }

        }
        // const receiveMsg = (data) => {
        //         let flag = true;
        //         let cunzaiNowChat = true;

    //         if (data.from + '' === chating.value.id + '') {
    //             flag = false;
    //             cunzaiNowChat = false;
    //             chating.value.msg.push(data);
    //         }
    //         let sortIndex = null;
    //         if (nowChatFriends.value) {
    //             nowChatFriends.value.forEach((item, index) => {
    //                 if (item.id + '' === data.from + '') {
    //                     cunzaiNowChat = false;
    //                     if (flag) {
    //                         item.unread += 1
    //                     }
    //                     item.msg.push(data);
    //                     item.overMsg = data.text;
    //                     item.time = new Date().toLocaleString();
    //                     sortIndex = index;

    //                 }
    //             })
    //         }

    //         if (cunzaiNowChat) {
    //             if (friendStore.friends) {
    //                 friendStore.friends.forEach(item => {
    //                     if (item.id + '' === data.from + '') {
    //                         let nowChatEntity = {
    //                             id: item.id,
    //                             img: item.img,
    //                             name: item.name,
    //                             time: data.time,
    //                             overMsg: data.text,
    //                             signature: item.signature,
    //                             unread: 1,
    //                             msg: []
    //                         }
    //                         nowChatEntity.msg.push(data)
    //                         nowChatFriends.value.unshift(nowChatEntity)
    //                         flag = false;
    //                     }


    //                 })
    //             }


    //         }
    //         //置顶
    //         if (flag) {
    //             let sortEntity = nowChatFriends.value[sortIndex];
    //             nowChatFriends.value.splice(sortIndex, 1);
    //             nowChatFriends.value.unshift(sortEntity);
    //         }
    //     }
    //关闭对话好友
    const delFrined = (id) => {
            const delIndex = nowChatFriends.value.forEach((item, index) => {
                if (item.id + '' === id + '') {
                    nowChatFriends.value.splice(index, 1);

                }
            })



            if (chating.value.id + '' === id + '') {
                chating.value.id = '';
                chating.value.name = '';
                chating.value.img = '';
                chating.value.msg = [];
            }
        }
        //添加好友打招呼消息
    const addHelloMsg = (data) => {
        console.log(data);
        let flag = true;
        nowChatFriends.value.forEach(item => {
            if (item.id + '' === data.id + '') {
                flag = false;
                item.overMsg = data.msg;
                item.time = data.time;
                item.msg.push(data);
                if (chating.value.id + '' === data.id + '') {
                    chating.value.msg.push(data);
                }
            }

        })
        if (flag) {
            friendStore.friends.forEach(item => {
                if (item.id + '' === data.id + '') {
                    let nowChatEntity = {
                        id: item.id,
                        img: item.img,
                        name: item.name,
                        time: data.time,
                        overMsg: data.text,
                        signature: item.signature,
                        unread: 1,
                        msg: []
                    }
                    let sendMsgEntity = {
                        from: data.id,
                        text: data.text,
                        time: data.time
                    }
                    nowChatEntity.msg.push(sendMsgEntity)
                    nowChatFriends.value.unshift(nowChatEntity)
                }

            })
        }

    }
    return {
        verifyMsg,
        setVerifyMsg,
        nowChatFriends,
        setNowChatFriends,
        removeNowChatFriendById,
        setNowChatFriensById,
        setNowChatTime,
        chating,
        setChating,
        removeunRead,
        setOffLineMsg,
        setSendMsg,
        receiveMsg,
        delFrined,
        setNewFriend,
        addHelloMsg
    }
}, {
    persist: true
})